<?php
/**
 * Created by PhpStorm.
 * User: bzyy
 * Date: 2015-7-22 19:25:49
 */

class PostexpressModel extends BaseModel{

    const RULE_FREE = 1;//包邮
    const RULE_DEFAULT = 2;//默认类型

    public $table = 'post_express';

    public $prikey = 'id';

    public $_autov = array(
        'name' => array(
            'required'  => true,
            'filter'    => 'trim',

        ),
        'postal_id' => array(
            'required'  => true,
            'filter'    => 'trim',
            'reg'   => '/\d+/'
        ),

    );

    public function unique($name,$postal_id,$not_id=0)
    {
        $cond = array();
        $cond['conditions'] = " name='{$name}' and postal_id='{$postal_id}' and id!='{$not_id}' ";
        $row = $this->get($cond);
        return empty($row);
    }

    /**
     * 编辑快递的多条规则的处理
     * @auth bzyy
     * 2015-7-23 15:31:56
     * -1
     */
    public function editpressrules($rules,$express_id)
    {
        //判断节点数据
        if(!isset($rules[self::RULE_FREE]) || !isset($rules[self::RULE_DEFAULT]))
        {
            $this->_error('请设置默认费用');
            return false;
        }
        //编辑数据
        //默认费用不可以为0

        $model = m('postexprule');
        $f = $model->editRules($rules,$express_id);
        if($f)
        {
            return true;
        }
        else
        {
            foreach($model->get_error() as $val)
            {
                list($k,$v) = $val;
                $this->_error($k,$v);
            }

            return false;
        }

    }

    /**
     * 取得所有的信息
     * @auth bzyy
     * 2015-7-24 18:01:43
     * 取得对应的快递公司
     * 并载入对应全部的
     */
    public function details($post_template_id)
    {
        $data = array();
        $conf = array();
        $conf['conditions'] = "postal_id='{$post_template_id}'";

        $temp_data = $this->findAll($conf);

        foreach($temp_data as $val)
        {
            $val['rules'] = array();
            $data[$val['id']] = $val;
        }

        //载入规则数据
        $keys = array_keys($data);
        $model = m('postexprule');
        $temp_press = $model->rules($keys);
        //echo '<pre>';print_r($temp_press);exit;
        foreach($data as $key=>$val)
        {
            if(!isset($temp_press[$key]))
            {
                continue ;
            }
            $data[$key]['rules'] = $temp_press[$key];
        }

        return $data;
    }

    /**
     * 添加数据的同时
     * 要同时添加，对应的默认规则和免费规则
     * @auth bzyy
     * 2015-7-24 18:46:02
     */
    public function add($data)
    {
        $flag = parent::add($data);
        if(!$flag)
        {
            return false;
        }

        //
        $free = array('express_id'=>$flag,'postal_id'=>$data['postal_id'],'store_id'=>$data['store_id'],'first_price'=>0,'step'=>0,'step_price'=>0,'ctype'=>1);
        $default = array('express_id'=>$flag,'postal_id'=>$data['postal_id'],'store_id'=>$data['store_id'],'first_price'=>-1,'step'=>-1,'step_price'=>-1,'ctype'=>2);

        $model = m('postexprule');
        $f = $model->add($free);
        if(!$f)
        {
            return false;
        }

        $f = $model->add($default);
        if(!$f)
        {
            return false;
        }

        return $flag;

    }

    /**
     * 保存对应的数据，注意可能是多条数据
     * @auth bzyy
     * 2015-7-24 19:37:23
     */
    public function editinfo($datas,$addon)
    {
        //检测是否存在不安全的因素
        //var_dump($datas);exit;
        $model = m('postexprule');
        $has_exp_ids = array();
        //编辑规则
        foreach($datas as $val)
        {
            //循环保存对应规则信息

            $info = array('name'=>$val['name'],'postal_id'=>$addon['postal_id'],'store_id'=>$addon['store_id']);
            if($val['type'] ==1)
            {
                $info['name'] = '默认快递';
            }
            $id = intval($val['id']);
            if($id && in_array($id,$addon['express_ids']))
            {
                $flag = $this->edit($id,$info);
                if(!$flag)
                {
                    return false;
                }
            }
            else
            {
                $flag = $this->add($info);
                if(!$flag)
                {
                    return false;
                }
                $id = $flag;
            }

            $addon['express_id'] = $id;
            $has_exp_ids[] = $id;
            $f = $model->editinfo($val['rules'],$addon);

            if(!$f)
            {
                $this->_errors = $model->_errors;
                return false;
            }

        }

        $has_exp_ids = array_filter($has_exp_ids);
        $has_exp_ids[] = 0;

        //清除的处理
        $sql = "select id from ".$this->table . " where postal_id='{$addon['postal_id']}' and store_id='{$addon['store_id']}' and id not in (". implode(',',$has_exp_ids) .") ";

        $rows = $this->getAll($sql);

        if(!empty($rows))
        {
            $row_ids = array();
            foreach($rows as $val)
            {
                $row_ids[] = $val['id'];
            }


            $flag = $this->del($row_ids);

            if(!$flag)
            {
                $this->_error('清理快递数据是发生错误');
                return false;
            }
        }




        return true;

    }

    /**
     * @param $ids
     * 找到rules向下删除
     */
    public function del($ids)
    {
        $ids[] = 0;
        //找到rules_id
        $sql = "select id from ".$this->_prefix."post_exp_rule where express_id in (". implode(',',$ids) .") ";
        $temp_data = $this->getAll($sql);

        if(!empty($temp_data))
        {
            $rules_ids = array();
            foreach($temp_data as $val)
            {
                $rules_ids[] = $val['id'];
            }

            $model = m('postexprule');
            $f = $model->del($rules_ids);
            if(!$f)
            {
                $this->_errors = $model->_errors;
                return false;
            }
        }
        $sql = "delete from ".$this->table." where id in (". implode(',',$ids) .") ";
        $f = $this->query($sql);
        if(!$f)
        {
            $this->_error('删除时发生错误');
            return false;
        }

        return true;

    }


} 